package com.wkk.service.impl;

import com.wkk.entity.*;
import com.wkk.mapper.ChatMapper;
import com.wkk.mapper.UserMapper;
import com.wkk.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChatServiceImpl implements ChatService {

    @Autowired
    private ChatMapper chatMapper;

    @Autowired
    private UserMapper userMapper;
    /**
     * 获取满足条件的朋友圈,并判断某条朋友圈是否被当前登录且调用本接口的用户点赞以及评论
     * @param uid 当前调用该接口的用户唯一标识符
     * @param condition 传递的条件
     * @return
     */
    @Override
    public User queryChatRecordByCondition(Integer uid,String condition,Integer fan){
        List<ChatRecord> chatRecords = null;
        User user = null;
        if(condition.equals("all")){//获取所有朋友圈
            user = new User();
            chatRecords = chatMapper.selectListRecord();
        }else if(condition.equals("attention")){//获取调用该接口关注的用户的朋友圈
            user = new User();
            chatRecords = chatMapper.selectListRecordByUidAttention(uid);
        }else if(condition.equals("one")){//获取某人的朋友圈
            user = userMapper.selectOne(uid);
            chatRecords = chatMapper.selectListRecordByUid(uid);
            user.setFanTrue(0);//初始化防止出现数据异常
            for(ChatUser chatUser:chatMapper.queryChatUsersByUid(uid)){
                if(chatUser.getFan()==fan){
                    user.setFanTrue(1);
                }
            }
            user.setChatCount(chatRecords.size());
            user.setFanCount(chatMapper.getFanCountOnStar(uid));
        }
        List<ChatComment> chatComments = chatMapper.selectCommentsByUid(uid);
        List<ChatLike> chatLikes = chatMapper.selectLikeByUid(uid);
        for(ChatRecord chatRecord : chatRecords){
            //将朋友圈对应的图片库一一对应
            List<ChatRecordPicture> chatRecordPictures = chatMapper.selectPictureByCRid(chatRecord.getCrid());
            chatRecord.setChatRedisPictures(chatRecordPictures);
            //将朋友圈与评论一一对应
            List<ChatComment> comments = chatMapper.selectCommentsByCRid(chatRecord.getCrid());
            chatRecord.setChatComments(comments);
            //将评论数赋值
            chatRecord.setCommentCount(comments.size());
            //将点赞数赋值
            chatRecord.setLikeCount(chatMapper.getLikeCountByCRid(chatRecord.getCrid()));
            //初始化，防止被其他用户数据覆盖而产生异常
            chatRecord.setCommentUid(0);
            chatRecord.setLikeUid(0);
            //判断该朋友圈是否已被当前调用该接口的用户评论
            for(ChatComment chatComment:chatComments){
                if(comments.contains(chatComment)){
                    chatRecord.setCommentUid(uid);
                }
            }
            //判断该朋友圈是否已被当前调用该接口的用户点赞
            for(ChatLike chatLike:chatLikes){
                if(chatLike.getCrid()==chatRecord.getCrid()){
                    chatRecord.setLikeUid(uid);
                }
            }
            chatRecord.setUser(userMapper.selectOne(chatRecord.getUid()));
        }
        user.setChatRecords(chatRecords);
        return user;
    }

    @Override
    public void likeOneChat(ChatLike chatLike) {
        chatMapper.insertLike(chatLike);
    }

    @Override
    public void dislikeOneChat(ChatLike chatLike) {
        chatMapper.deleteLike(chatLike);
    }

    @Override
    public void attentionOne(ChatUser chatUser) {
        chatMapper.insertFan(chatUser);
    }

    @Override
    public void notAttentionOne(ChatUser chatUser) {
        chatMapper.deleteFan(chatUser);
    }

    @Override
    public boolean addComment(ChatComment chatComment) {
        int i = 0;
        i = chatMapper.insertComment(chatComment);
        return i == 1?true:false;
    }

    @Override
    public boolean deleteComment(Integer ccid) {
        int i = 0;
        i = chatMapper.deleteCommentByCCid(ccid);
        return i == 1?true:false;
    }

    @Override
    public List<ChatComment> queryCommentByUid(Integer uid) {
        return chatMapper.selectCommentsByUid(uid);
    }

    @Override
    public void addChatRecord(ChatRecord chatRecord) {
        chatMapper.insertRecord(chatRecord);
        for(ChatRecordPicture chatRecordPicture:chatRecord.getChatRedisPictures()){
            chatMapper.insertRPicture(chatRecordPicture);
        }
    }

    @Override
    public void deleteChatRecord(Integer crid) {
        chatMapper.deleteCommentByCRid(crid);
        chatMapper.deleteRPictureByCRid(crid);
        chatMapper.deleteRecord(crid);
    }

    @Override
    public void restrictOne(Integer uid, Integer level) {

    }

    @Override
    public void releaseOne(Integer uid) {

    }

    @Override
    public Integer selectLevelByUid(Integer uid) {
        return null;
    }
}
